<?php

class rglStrCsv {
	public $sFile="";
	public $posFile= 0;
	public $sMode='r';
	public $bHeader=false;
	public $sDelim=";";
	public $sEnclos='"';
	public $desEsito='OK';
	public $codEsito='200';
	
	private $fh=null;
	private $aCampi=array();
	private $bHeaderWritten=false;

   public function __construct($sFile='', $sMode='	w', $bHeader=false, $sDelim=';',$sEnclos='"') {
   	return $this->open($sFile, $sMode, $bHeader, $sDelim, $sEnclos);
   }
   
   public function open($sFile='', $sMode='w', $bHeader=false, $sDelim=';',$sEnclos='"') {
   	$this->sFile = $sFile;
   	$this->posFile = 0;
   	$this->sMode = $sMode;
   	$this->bHeader = $bHeader;
   	$this->sDelim = substr($sDelim,0,1);
   	$this->sEnclos = substr($sEnclos,0,1);
   	$this->bHeaderWritten = false;
   	$this->aCampi = array();
   }

   public function read() {
   	if ($this->sMode != 'r') {
   		$this->desEsito = sprintf("modalita' '$s' - non aperto in lettura",$this->sFilePath,$this->sMode);
   		$this->codEsito = '502';
   		return false;
   	}
   	if ($this->posFile >= strlen($this->file)) {
   		return false;
   	}
   	if ($this->bHeader) return $this->readH();
   	$posX = $this->posFile;
   	$posFinRec = strpos($this->sFile,"\n",$posX);
   	if ($posFinRec === false) $posFinRec = strlen($this->sFile);
   	$aRec = $this->parseCsvRec(subtsr($this->sFile,$posX,$posFinRec));
   	$this->posFile = $posFinRec + 1;
   	return $aRec;
   }
   
   public function write($aRec) {
   	if ($this->sMode != 'w') {
   		$this->desEsito = sprintf("modalita' '$s' - non aperto in scrittura",$this->sFilePath,$this->sMode);
   		$this->codEsito = '504';
   	}
   	if ($this->bHeader and !$this->bHeaderWritten) {
   		$this->aCampi = array();
   		$sDelim = "";
   		foreach($aRec as $nX => $vX) {
   			$this->aCampi[$nX] = "";
   			$nX = STR_replace("\n","",$nX);
   			$nX = STR_replace($this->sEnclos," ",$nX);
	   		$this->sFile .= $sDelim.$this->sEnclos.$nX.$this->sEnclos;
	   		$sDelim = $this->sDelim;
   		}
   		$this->sFile .= "\r\n";
   		$this->bHeaderWritten = true;
   	}
  		$sDelim = "";
   	foreach($this->aCampi as $nX => $vX) {
   		$sVal= (isset($aRec[$nX]))?$aRec[$nX]:"";
			$sVal = STR_replace("\n","",$sVal);
			$sVal = STR_replace($this->sEnclos," ",$sVal);
   		$this->sFile .= $sDelim.$this->sEnclos.$sVal.$this->sEnclos;
   		$sDelim = $this->sDelim;
    	}
  		$this->sFile .= "\r\n";
   }
   
   public function close() {
   	$this->posFile = strlen($this->sFile);
   	return true;
   }
   
   private function readH() {
	  	if (count($this->aCampi) == 0) {
	   	$posX = $this->posFile;
	   	$posFinRec = strpos($this->sFile,"\n",$posX);
	   	if ($posFinRec === false) $posFinRec = strlen($this->sFile);
	   	$aCampi = $this->parseCsvRec(subtsr($this->sFile,$posX,$posFinRec));
   		$this->aCampi = array();
   		foreach($aCampi as $kk => $nome) {
   			$nome = trim($nome);
   			if ($nome == "") $nome = "x";
	   		$nn = 2;
	   		$suff = "";
   		 	while(isset($this->aCampi[$nome.$suff])) {
   		 		$suff = $nn;
   		 		$nn++;
   		 	}
   		 	$this->aCampi[$nome.$suff] = "";
   		}
   		$this->posFile = $posFinRec + 1;
   	}
   	$posX = $this->posFile;
   	$posFinRec = strpos($this->sFile,"\n",$posX);
   	if ($posFinRec === false) $posFinRec = strlen($this->sFile);
   	$aVal = $this->parseCsvRec(subtsr($this->sFile,$posX,$posFinRec));
   	$aRet = $this->aCampi;
   	$kk=0;
   	foreach($aRet as $nX => $vX) {
   		if (!isset($aVal[$kk])) break;
   		$aRet[$nX] = $aVal[$kk];
   		$kk++;
     	}
   	$this->posFile = $posFinRec + 1;
  		return $aRet;
   }
   
   public function parseCsvRec($sRec) {
   	$posX = 0; 
   	$posFinRec = strlen($sRec); 
   	$aRec = array();
   	while($posX < $posFinRec) {
   		while($posX < $posFinRec and preg_match("/[ \r]/",$sRec[$posX]) ) $posX++;
   		if ($posX >= $posFinRec) break;
   		if ($sRec[$posX] == $this->sEnclos) {
   			$sEnclos = $this->sDelim;
   			$posX++;
   		} else {
   			$sEnclos = "";
   		}
   		if ($posX >= $posFinRec) break;
   		if ($sEnclos == "") {
   			$posFinCam = strpos($sRec,$this->sDelim,$posX);
   		} else {
   			$posFinCam = strpos($sRec,$sEnclos,$posX);
   		}
   		if ($posFinCam === false) {
   			$posFinCam = $posFinRec;
   		}
   		$aRec[] = trim(substr($sRec,$posX,$posFinCam-$posX));
   		if ($sEnclos != "") {
   			$posFinCam = strpos($sRec,$this->sDelim,$posX);
	   		if ($posFinCam === false) {
	   			$posFinCam = $posFinRec;
	   		}
   		}
   		$posX++;
   		while($posX < $posFinRec and preg_match("/[ \r]/",$sRec[$posX]) ) $posX++;
   	}
   	return $aRec;
   } 
}
?>